#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/print_sys_info.sh"

APP_INSTALL_NAME="gpt-sovits"
APP_TARGET_DIR_NAME="gpt-sovits"

export CURRENT_APP_PATH="$LMD_APPS_DIR/$APP_TARGET_DIR_NAME"
echo "App Dir: ${CURRENT_APP_PATH}"

cd "$CURRENT_APP_PATH"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

export GRADIO_TEMP_DIR="$CURRENT_APP_PATH/gradio_temp"
CONDA_PREFIX="./venv"


if [ "$LMD_LAUNCH_APP_OPT_USE_GPU" = "false" ]; then
  export CUDA_VISIBLE_DEVICES="-1"
fi

echo "CUDA_VISIBLE_DEVICES $CUDA_VISIBLE_DEVICES"

export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home"
export MODELSCOPE_HOME="$CURRENT_APP_PATH/tmp_modelscope_home"
export MODELSCOPE_CACHE="$CURRENT_APP_PATH/tmp_modelscope_cache"
export PYTORCH_ENABLE_MPS_FALLBACK=1

cp webui.py webui_temp.py

LOCALE=""

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
  echo "macOS. LMD_LOCALE: $LMD_LOCALE"
  if [ "$LMD_LOCALE" = "zhHans" ]; then
    LOCALE="zh_CN"
  fi
fi

# replace
conda_run_cmd python ${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py "webui_temp.py" "server_port=webui_port_main," ""
conda_run_cmd python ${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py "webui_temp.py" "os.environ[\"HF_ENDPOINT\"] = \"https://hf-mirror.com\"" ""

APP_FILE="webui_temp.py"

conda run -p $CONDA_PREFIX --no-capture-output python $APP_FILE $LOCALE

